package org.wtframework.web.demo.test;

import java.util.Date;
import java.util.List;

import org.openredhead.springx.core.server.manager.IBasicUserManager;
import org.openredhead.springx.core.server.model.IUser;
import org.openredhead.springx.core.server.util.DateUtils;
import org.openredhead.springx.core.web.mvc.ajaxrpc.BasicAjaxRpcRemoteService;
import org.openredhead.springx.core.web.mvc.annotation.AjaxRpcMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wtframework.platform.server.model.impl.User;

import com.google.common.collect.Lists;

@Repository
// @AnonymousForbidden
public class AjaxRpcTestActionController extends BasicAjaxRpcRemoteService {

	@Autowired
	private IBasicUserManager<User> userManager;

	@AjaxRpcMethod
	// @AnonymousForbidden
	public Integer getInteger() {
		System.out.println("Ajax Rpc 信息：获取Integer，值为：" + 112);
		return 112;
	}

	@AjaxRpcMethod
	public void setInteger(Integer object) {
		System.out.println("Ajax Rpc 信息：设置Integer，设置值为：" + object);
	}

	@AjaxRpcMethod
	public Date getDate() {
		return new Date();
	}

	@AjaxRpcMethod
	public void setDate(Date date) {
		System.out.println("Ajax Rpc 信息：设置Date，设置值为：" + DateUtils.getDateTimeString(date));
	}

	@AjaxRpcMethod
	public IUser getUser() {

		IUser user = userManager.load(1L);

		return user;

	}

	@AjaxRpcMethod
	public List<User> getUserList() {

		List<Long> idList = Lists.newArrayList();

		idList.add(1L);
		idList.add(2L);
		idList.add(30L);

		return userManager.loadList(idList, false);

	}

	@AjaxRpcMethod
	public void setUser(User user) {
		System.out.println("Ajax Rpc 信息：设置User，id设置值为：" + user.getId());
		System.out.println("Ajax Rpc 信息：设置User，username设置值为：" + user.getUsername());
		System.out.println("Ajax Rpc 信息：设置User，establishDate设置值为：" + user.getEstablishDate());
	}

	@AjaxRpcMethod
	public void setUserList(List<User> userList) {

		for (IUser user : userList) {

			System.out.println("Ajax Rpc 信息：设置User，id设置值为：" + user.getId());
			System.out.println("Ajax Rpc 信息：设置User，username设置值为：" + user.getUsername());
			System.out.println("Ajax Rpc 信息：设置User，establishDate设置值为：" + user.getEstablishDate());
		
		}

	}

	public void spClass(Object o) {

		System.out.println(o == null ? "null" : o);

	}

	@AjaxRpcMethod
	// @AnonymousForbidden
	public void setObjectList(User o1, List<String> o2, int o3, double o4, boolean o5, Object o6) {

		spClass(o1);
		spClass(o2);

		for (String tmpO : o2)
			spClass(tmpO);

		spClass(o3);
		spClass(o4);
		spClass(o5);
		spClass(o6);

	}

	@AjaxRpcMethod
	public void getError() throws Exception {
		
		Exception e = new Exception("dsdfkjsdklf s");
		System.out.println("--------------------");
		System.out.println(e.getMessage());
		throw e;
//		Integer.parseInt("asdfadf");
	}

}
